CatalogManager = Ext.extend(Ext.Window, {
	formPanel : null,
	westPanel : null,
	northPanel:null,
	constructor : function(a) {
		Ext.applyIf(this, a);
		this.initUIComponents();
		CatalogManager.superclass.constructor.call(this, {
			layout : "border",
			tbar:null,
			items : [  this.northPanel,this.westPanel,this.formPanel],
			border : false,
			modal : true,
			plain : true,
			id : "BookCatalogWin",
			title : "书籍章节信息",
			iconCls : "menu-role",
			width : document.body.clientWidth - 300,
			height : document.body.clientHeight,
			bodyStyle : "padding:5px;",
			buttonAlign : "center",
			buttons : this.buttons
		});
	},
	initUIComponents : function() {
		var catalog = this.bookName;
		var bookid = this.bookid;
		var mapToolbar = new Ext.Toolbar({  
            items: [{  
                xtype: 'button',  
                toggleGroup: 'toolbarGroup',  
                text: '添加新章节',                  
                scale: 'large',  
                handler : function() {
                	CatalogManager.ajaxCatalog();
                }
            }, '-', {  
                xtype: 'button',  
                toggleGroup: 'toolbarGroup',  
                text: '删除当前章节',  
                scale: 'large',  
                handler : function() {
                	var catalogId=Ext.getCmp('CatalogForm').getForm().findField('catalogId').getValue();
                	if(catalogId==""){
                		return false;
                	}
                	Ext.Msg.confirm("信息确认", "您确认要删除该章节吗？", function(c) {
	                		if (c == "no") {
	                			return
	                		}
		                	Ext.Ajax.request({  
		        				url : __ctxPath + "/deleteCatalog.do?bookid=" + catalogId,
		                        method : 'POST',  
		                        success : function(response) {  
		                        	Ext.ux.Toast.msg("操作信息","章节成功删除");
		                        	CatalogManager.ajaxCatalog();
		                        	Ext.getCmp("catalogTree").root.reload();
		                        },  
		                        failure : function() {  
		                        	Ext.ux.Toast.msg("操作信息","方法调用失败");
		                        }  
		                    })
                	})
                }
            }]
		});
		var radioTryRead = new Ext.form.RadioGroup({
		    fieldLabel : "试阅读章节",
		    labelWidth : 65,
		    id:"radioGroup",
		    items : [
		             {boxLabel : '是',name : "catalog.tryReading",inputValue : "1",value:"1",checked : true}, 
		             {boxLabel : '否',name : "catalog.tryReading",inputValue : "0",value:"0"}
		             ]
		});
		var root = new Ext.tree.AsyncTreeNode({
			text : catalog,
			id : bookid,
			expanded : true
		});
		var loader = new Ext.tree.TreeLoader({
			dataUrl : __ctxPath + '/searchCatalog.do?bookid=' + bookid,
			requestMethod : 'POST'
		});
		CatalogManager.ajaxCatalog = function(catalogId){
			Ext.Ajax.request({  
				url : __ctxPath + "/getCatalog.do?bookid=" + catalogId,
                method : 'POST',  
                success : function(response) {  
                        var result = Ext.util.JSON.decode(response.responseText);  
                        if(result.success){
                        	Ext.getCmp("CatalogForm").getForm().setValues( { 'catalogId' : result.data.catalogId } );  
                        	Ext.getCmp("CatalogForm").getForm().setValues( { 'catalog' : result.data.catalog } );  
                        	Ext.getCmp("CatalogForm").getForm().setValues( { 'catalogName' : result.data.catalogName } );
                        	Ext.getCmp("CatalogForm").getForm().setValues( { 'catalogContentStr' : result.data.catalogContentStr } );
                        	radioTryRead.setValue(result.data.tryReading);  
                        }
                },  
                failure : function() {  
                    Ext.Msg.alert("提示", "方法调用失败");  
                }  
            })
		}
		this.northPanel = new Ext.Panel({  
            id: 'northPanel',  
            region: 'north',  
            tbar: null,   
            autoScroll: false,              
            border: true,  
            autoHeight: true,  
            collapsible: false,  
            titleCollapse: false,  
            margins: '0 0 0 0'  
        }); 
		this.westPanel = new Ext.tree.TreePanel({
			root : root,
			title : '<span>' + catalog + '章节</span>',
			region : 'west',
			id : 'catalogTree',
			split : true,
			width : 220,
			height : document.body.clientHeight - 130,
			collapsible : true,
			autoScroll : true,
			animate : false,
			loader : loader,
			tbar:mapToolbar,
			listeners : {
				"click" : function(node) {
					if(node.id==bookid){
						Ext.ux.Toast.msg("操作信息","请点解章节");
						return false
					}
					CatalogManager.ajaxCatalog(node.id);
				},
				"beforeload" : function(node) {
					if (node.id != '') {
						Ext.getCmp("catalogTree").loader.dataUrl =  __ctxPath+'/searchCatalog.do?bookid='+node.id
					}
				}
			}
		});
		this.formPanel = new Ext.FormPanel({
			url : __ctxPath + "/saveCatalog.do",
			id : "CatalogForm",
			height : document.body.clientHeight - 130,
			region : "center",
			autoHeight : true,
			frame : true,
			labelWidth : 65,
			labelAlign : "center",
			items : [{ // 行1  
		        layout : "column", // 从左往右的布局  
		        items : [{  
		            columnWidth : .3, // 该列有整行中所占百分比  
		            layout : "form", // 从上往下的布局  
		            items : [
		                     {name : "catalog.catalogId",id : "catalogId",xtype : "hidden"},
		                     {name : "catalog.bookid",id : "bookid",xtype : "hidden",value:bookid},
		                     {name:"catalog.catalog",id:"catalog",xtype : "textfield",fieldLabel : "章节", anchor : '90%' }]  
		        }, {  
		            columnWidth : .3,  
		            layout : "form",  
		            items : [{name:"catalog.catalogName",id:"catalogName",xtype : "textfield",  fieldLabel : "章节名称", anchor : '90%' }]  
		        }, {  
		            columnWidth : .3,  
		            layout : "form",  
		            items : [radioTryRead]  
		        }]  
		    }, {
		        layout : "form", 
		        items : [{  
		            xtype : "htmleditor",  
		            fieldLabel : "内容", 
		            name:"catalog.catalogContentStr",
		            id:"catalogContentStr",
		            height : document.body.clientHeight-400,
		            anchor: "98%"  
		        }]  
		    }]
		});
		this.buttons = [{
				text : "提交",
				iconCls : "btn-save",
				handler : function() {
					var b = Ext.getCmp("CatalogForm");
					if (b.getForm().isValid()) {
						b.getForm().submit({
								method : "post",
								waitMsg : "正在提交数据...",
								success : function(e,f) {
									Ext.ux.Toast.msg("操作信息","信息保存成功！");
									Ext.getCmp("catalogTree").root.reload();
								},
								failure : function(e,f){
									Ext.MessageBox.show({
												title : "操作信息",
												msg : "信息保存出错，请联系管理员！",
												buttons : Ext.MessageBox.OK,
												icon : "ext-mb-error"
											});
								}
							});
					}
				}
			}]
	}
});
